package impl

import (
	"context"
	"go-caipu/pkg/services/admin/systemlog"
	userModel "go-caipu/pkg/services/admin/sysuser"
)

func (s *Service) GetSysOperaLogPage(ctx context.Context, cmd systemlog.GetSysOperaLogPageCommand) (result []systemlog.SysOperaLog, num int64, err error) {
	result, num, err = s.store.GetSysOperaLogPage(ctx, cmd)
	if err != nil {
		return
	}
	return
}

type swaggerJson struct {
	Paths map[string]interface{} `json:"paths"`
}
type operationJson struct {
	API       string   `json:"api"`
	Method    string   `json:"method"`
	BodyKeys  []string `json:"bodyKeys"`
	ParamKeys []string `json:"paramKeys"`
	//BeforeFunctions []functionInfo `json:"beforeFunctions"`
	FormatZH string `json:"formatZH"`
	FormatEN string `json:"formatEN"`
}

// InsertOperLog 插入操作日志
func (s *Service) InsertOperLog(ctx context.Context, cmd *systemlog.SysOperaLog) error {
	//记录操作人
	if cmd.CreateBy > 0 {
		user, err := s.userService.GetUser(ctx, userModel.GetUserCommand{UserId: cmd.CreateBy})
		if err != nil {
			return nil //error 不影响业务
		}
		if cmd.StatusCode == 200 {
			cmd.StatusCode = 1
		}
		cmd.CreateUser = user.NickName
	}
	 
	// 从缓存中获取API列表
	apiList, err := s.apiService.GetSysApiList(ctx)
	if err != nil {
		cmd.Module = "api接口失败"
		cmd.Description = err.Error()
	}
	//根据路径和方法获取模块和方法
	module, method := getModuleFunc(apiList, cmd.RequestUrl, cmd.Method)
	cmd.Module = module
	cmd.Description = method

	return s.store.InsertOperLog(ctx, cmd)
}
func (s *Service) GetSysOperaLog(ctx context.Context, cmd systemlog.GetSysOperaLogCommand) (result systemlog.SysOperaLog, err error) {
	return s.store.GetSysOperaLog(ctx, cmd)
}
